package com.huitone.gddw.service.impl.analysis;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.analysis.ICallTestPhoneDao;
import com.huitone.gddw.dao.entity.CallTestPhone;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.analysis.ICallTestPhoneService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 拨测号码 接口实现类
 *
 */
@Service
public class CallTestPhoneService implements ICallTestPhoneService {

	@Autowired
	private ICallTestPhoneDao callTestPhoneDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<CallTestPhone> selectPage(QueryPage<CallTestPhone> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("testPhone", page.getParams().get("testPhone"));
		map.put("username", page.getParams().get("username"));
		map.put("status", page.getParams().get("status"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<CallTestPhone> result = callTestPhoneDao.selectPage(map);
		int totalCount = callTestPhoneDao.selectCount(map);
		
		QueryPage<CallTestPhone> queryPage = new QueryPage<CallTestPhone>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public CallTestPhone selectByPrimaryKey(String recId) {
		return callTestPhoneDao.selectByPrimaryKey(recId);
	}

	@Override
	public CallTestPhone toUpdate(String recId) {
		return this.selectByPrimaryKey(recId);
	}

	@Override
	public void changeStatus(String recId, String status) {
		callTestPhoneDao.changeStatus(recId,status);
	}

	@Override
	public void save(CallTestPhone entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		callTestPhoneDao.insertSelective(entity);
		
		this.updateParamRec("保存拨测号码");
	}

	@Override
	public void update(CallTestPhone entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		callTestPhoneDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新拨测号码");
	}

	@Override
	public void delete(String recId) {
		callTestPhoneDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除拨测号码");
	}

	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.BLACKUSE, desc);
	}

	@Override
	public List<CallTestPhone> getRecord(Map<String, Object> params) {
		List<CallTestPhone> list = callTestPhoneDao.getRecord(params);
		return list;
	}
}
